package bsh;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;

/**
 * {@link ClassVisitor} that calls all of its methods on two ClassVisitors
 * 
 * @author takeshi
 */
public class DoubleClassVisitor implements ClassVisitor {
    private final ClassVisitor cv1, cv2;

    /**
     * C'tor
     * 
     * @param cv1 {@link ClassVisitor}
     * @param cv2 the other {@link ClassVisitor}
     */
    public DoubleClassVisitor(ClassVisitor cv1, ClassVisitor cv2) {
        this.cv1 = cv1;
        this.cv2 = cv2;
    }

    /**
     * {@inheritDoc}
     */
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        this.cv1.visit(version, access, name, signature, superName, interfaces);
        this.cv2.visit(version, access, name, signature, superName, interfaces);
    }

    /**
     * {@inheritDoc}
     */
    public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
        return new DoubleAnnotationVisitor(this.cv1.visitAnnotation(desc, visible), this.cv2.visitAnnotation(desc, visible));
    }

    /**
     * {@inheritDoc}
     */
    public void visitAttribute(Attribute attrs) {
        this.cv1.visitAttribute(attrs);
        this.cv2.visitAttribute(attrs);
    }

    /**
     * {@inheritDoc}
     */
    public void visitEnd() {
        this.cv1.visitEnd();
        this.cv2.visitEnd();
    }

    /**
     * {@inheritDoc}
     */
    public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
        FieldVisitor fv1 = this.cv1.visitField(access, name, desc, signature, value);
        FieldVisitor fv2 = this.cv2.visitField(access, name, desc, signature, value);
        return new DoubleFieldVisitor(fv1, fv2);
    }

    /**
     * {@inheritDoc}
     */
    public void visitInnerClass(String name, String outerName, String innerName, int access) {
        this.cv1.visitInnerClass(name, outerName, innerName, access);
        this.cv2.visitInnerClass(name, outerName, innerName, access);
    }

    /**
     * {@inheritDoc}
     */
    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
        MethodVisitor mv1 = this.cv1.visitMethod(access, name, desc, signature, exceptions);
        MethodVisitor mv2 = this.cv2.visitMethod(access, name, desc, signature, exceptions);
        return new DoubleMethodVisitor(mv1, mv2);
    }

    /**
     * {@inheritDoc}
     */
    public void visitOuterClass(String owner, String name, String desc) {
        this.cv1.visitOuterClass(owner, name, desc);
        this.cv2.visitOuterClass(owner, name, desc);
    }

    /**
     * {@inheritDoc}
     */
    public void visitSource(String source, String debug) {
        this.cv1.visitSource(source, debug);
        this.cv2.visitSource(source, debug);
    }
}
